//
//  MLWelcomeViewController.swift
//  MLWeiBo
//
//  Created by wenxing on 2017/1/7.
//  Copyright © 2017年 Se7venX. All rights reserved.
//

import UIKit
import SnapKit

let marginBottom:CGFloat = 120


class MLWelcomeViewController: UIViewController {

    
    var userAccount:MLUserAccount? {
        
        return MLUserAccountViewModel.shared.userAccount
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()


        view.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
    
        setupUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let margin = -(UIScreen.main.bounds.size.height - marginBottom - iconView.bounds.size.height)
        
        iconView.snp.updateConstraints { (make) in
            
            
            make.bottom.equalTo(margin)

        }
        
        
        UIView.animate(withDuration: 0.05, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 2, options: [], animations: {
        (isSuc) in
            
            self.view.layoutIfNeeded()
            
        }, completion: {
        
            (isSuc) in
            
            UIView.animate(withDuration: 0, animations: {
             
                self.nameLab.alpha = 1
                
            }, completion: { (isSuc) in
                
                UIApplication.shared.keyWindow?.rootViewController = MLTabBarController()
            
            })
        })
        
    }
    
    //MARK:- 设置UI -
    private func setupUI() {
        
        view.addSubview(iconView)
        view.addSubview(nameLab)
        nameLab.textAlignment = .center
        nameLab.text = userAccount?.name ?? ""
        let url = URL(string: userAccount?.avatar_large ?? "")
        
        iconView.setImageWith(url!, placeholderImage: #imageLiteral(resourceName: "avatar_default"))
     
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(-marginBottom)
        }
        
        nameLab.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp.bottom).offset(30)
        }
        
        nameLab.alpha  = 0
        
        
        
    }

    
    lazy var iconView: UIImageView = {
        
        let iv = UIImageView()

        iv.layer.cornerRadius = 85*0.5
        
        iv.layer.masksToBounds = true
        
        return iv
    }()
    
    lazy var nameLab = UILabel(title: "", titleColor: UIColor.darkGray, fontSize: 18)
    
    

}
